/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    tim.c
  * @brief   This file provides code for the configuration
  *          of the TIM instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "timer5.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

TIM_HandleTypeDef htim5;
TIMEVAL last_counter_var =0;
TIMEVAL elapsed_time = 0;
/* TIM14 init function */
void MX_TIM5_Init(void)
{

  /* USER CODE BEGIN TIM14_Init 0 */

  /* USER CODE END TIM14_Init 0 */

  /* USER CODE BEGIN TIM14_Init 1 */

  /* USER CODE END TIM14_Init 1 */
  htim5.Instance = TIM5;
  htim5.Init.Prescaler = 840-1;   //freq 84MHZ 840/84000000 =1/100000s=10us
  htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim5.Init.Period = 65535;
  htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim5) != HAL_OK)
  {
    Error_Handler();
  }

    // 清除中断标志
    __HAL_TIM_CLEAR_IT(&htim5, TIM_IT_UPDATE);

    // 启动定时器
    HAL_TIM_Base_Start_IT(&htim5);

    // 设置初始计数器值（避免从 0 开始）
    __HAL_TIM_SET_COUNTER(&htim5, 1);
  /* USER CODE BEGIN TIM14_Init 2 */

  /* USER CODE END TIM14_Init 2 */

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM5)
  {
  /* USER CODE BEGIN TIM14_MspInit 0 */

  /* USER CODE END TIM14_MspInit 0 */
    /* TIM14 clock enable */
    __HAL_RCC_TIM5_CLK_ENABLE();

    /* TIM14 interrupt Init */
    HAL_NVIC_SetPriority(TIM5_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(TIM5_IRQn);
  /* USER CODE BEGIN TIM14_MspInit 1 */

  /* USER CODE END TIM14_MspInit 1 */
  }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{

  if(tim_baseHandle->Instance==TIM5)
  {
  /* USER CODE BEGIN TIM14_MspDeInit 0 */

  /* USER CODE END TIM14_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_TIM5_CLK_DISABLE();

    /* TIM14 interrupt Deinit */
    HAL_NVIC_DisableIRQ(TIM5_IRQn);
  /* USER CODE BEGIN TIM14_MspDeInit 1 */

  /* USER CODE END TIM14_MspDeInit 1 */
  }
}

// 设置下一次定时器报警时间
void setTimer(TIMEVAL value)
{
    uint32_t timer = __HAL_TIM_GET_COUNTER(&htim5);  // 获取当前计数器值
    elapsed_time += timer - last_counter_var;
    last_counter_var = 65535 - value;
    
    // 直接设置计数器值（无需停止定时器）
    __HAL_TIM_SET_COUNTER(&htim5, 65535 - value);
}

// 获取经过的时间
TIMEVAL getElapsedTime(void)
{
    uint32_t timer = __HAL_TIM_GET_COUNTER(&htim5);
    if (timer < last_counter_var)
        timer += 65535;
    
    TIMEVAL elapsed = timer - last_counter_var + elapsed_time;
    return elapsed;
}

// TIM3 中断服务函数（由 HAL 处理）
void TIM5_IRQHandler(void)
{
    HAL_TIM_IRQHandler(&htim5);  // 必须调用 HAL 的中断处理 
}

// HAL 定时器中断回调函数（用户逻辑）
// 正确的定时器中断回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == TIM5) 
    {
        last_counter_var = 0;
        elapsed_time = 0;
        TimeDispatch();  // 回调中处理用户逻辑
    }
}
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
